package xin.petstore.framework.utils;

import java.math.BigDecimal;

public class Amount {
    private BigDecimal value;
    public static int scale = 10;

    public Amount(double value) {
        this.value = new BigDecimal(Double.toString(value));
    }

    public Amount(String value) {
        this.value = new BigDecimal(value);
    }

    public BigDecimal getValue() {
        return this.value;
    }

    public static double add(double v1, double v2) {
        Amount a1 = new Amount(v1);
        Amount a2 = new Amount(v2);
        return add(a1, a2);
    }

    public static double div(double v1, double v2) {
        Amount a1 = new Amount(v1);
        Amount a2 = new Amount(v2);
        return divide(a1, a2);
    }

    public static double sub(double v1, double v2) {
        Amount a1 = new Amount(v1);
        Amount a2 = new Amount(v2);
        return subtract(a1, a2);
    }

    public static double mul(double v1, double v2) {
        Amount a1 = new Amount(v1);
        Amount a2 = new Amount(v2);
        return multiply(a1, a2);
    }

    public static double add(Amount v1, Amount v2) {
        return v1.getValue().add(v2.getValue()).doubleValue();
    }

    public static double divide(Amount v1, Amount v2) {
        if (scale < 0) {
            throw new IllegalArgumentException("精度指定错误,请指定一个>=0的精度");
        }
        return v1.getValue().divide(v2.getValue(), scale,
                4).doubleValue();
    }

    public static double multiply(Amount v1, Amount v2) {
        return v1.getValue().multiply(v2.getValue()).doubleValue();
    }

    public static double subtract(Amount v1, Amount v2) {
        return v1.getValue().subtract(v2.getValue()).doubleValue();
    }

    public double doubleValue() {
        return getValue().doubleValue();
    }

    public void setScale(int scale) {
        scale = scale;
    }
}

